package com.yytek.common.core.utils;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

public class IotJson {
	public static <T> T fromJson(String json, Class<T> classOfT) {
		return JSON.parseObject(json, classOfT);
	}

	public static <T> T fromJson(String json, Type typeOfT) {
		return JSON.parseObject(json, typeOfT);
	}

/*	public static String toListJson(Object src) {

		return JSONArray.toJSONString(src);
	}*/

	public static String toJson(Object src) {
		return JSON.toJSONString(src);
	}


	public static String toJson(Object src, Type typeOfSrc) {
		return JSON.toJSONString(src);
	}

	public static <T> T fromJson(Object src, Class<T> classOfT) {
		return JSON.parseObject(JSON.toJSONString(src), classOfT);
	}

	public static Object[] fromJson(String text) {
		JSONArray list = JSON.parseArray(text);
		return list.toArray();

	}

	public static <T> List<T> fromJsonToList(String text, Class<T> typeOfSrc) {
		return JSON.parseArray(text, typeOfSrc);

	}

	public static String toJson(Object src, Map<String, Object> map) {

		JSONObject obj = JSON.parseObject(JSON.toJSONString(src));
		obj.putAll(map);
		return obj.toJSONString();
	}

	public static String toJson(Object src, String exKey, Object exValue) {

		JSONObject obj = JSON.parseObject(JSON.toJSONString(src));
		obj.put(exKey, exValue);
		return obj.toJSONString();
	}

	public static <T> List<T> fromMapToList(List<Map<String,Object>> data ,Class<T> typeTo){
		String json = toJson(data);
		return fromJsonToList(json, typeTo);
	}
}
